package com.bby.screentool.config;

import cn.hutool.core.util.ObjectUtil;
import com.bby.screentool.controller.MWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class Context {
    public static Map<String, String> idCache = new HashMap<>();
    public static Map<String, String> sessionIdCache = new HashMap<>();
    public static ConcurrentHashMap<String, MWebSocket> webSockets = new ConcurrentHashMap<>();
    // 用来存在线连接用户信息
    public static ConcurrentHashMap<String, MWebSocket> recorders = new ConcurrentHashMap<>();

    public static void addWebSocket(String screenId, MWebSocket mWebSocket) {
        webSockets.put(screenId,mWebSocket);
        log.debug("【websocket消息】有新的连接，总数为:" + webSockets.size());
    }

    public static void removeWebSocket(String screenId) {
        if (ObjectUtil.isEmpty(screenId)){
            return;
        }
        webSockets.remove(screenId);
        sessionIdCache.remove(idCache.remove(screenId));
        log.debug("【websocket消息】连接断开，总数为:" + webSockets.size());
    }

    public static MWebSocket getSocketServer(String screenId) {
        if (ObjectUtil.isEmpty(screenId)) return null;
        return webSockets.get(screenId);
    }

    public static void addRecorder(MWebSocket from) {
        recorders.put(from.getScreenId(),from);
    }

    public static boolean isRecorder(MWebSocket target) {
        return recorders.containsKey(target.getScreenId());
    }
}
